package org.example.day20240903文件操作.jinyong;

import java.io.*;
import java.util.*;

public class Main1 {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("金庸07鹿鼎记人物关系.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        List<relation> relationList = new ArrayList<>();
        Map<relation,Integer> relationIntegerMap = new HashMap<>();
        String line = bufferedReader.readLine();
        while (line != null){
            String[] str = line.split(" ");
            for (int i = 0; i<str.length; i++){
                for (int j = i+1; j< str.length; j++){
                    relation relation = new relation(str[i],str[j]);
                    relationList.add(relation);
                }
            }
            line = bufferedReader.readLine();
        }
        for (relation relation : relationList){
            if(relationIntegerMap.containsKey(relation)){
                int count = relationIntegerMap.get(relation)+1;
                relationIntegerMap.put(relation,count);
            }else {
                relationIntegerMap.put(relation,1);
            }
        }
        FileWriter fileWriter1 = new FileWriter("金庸鹿鼎记人物关系(单人).txt");
        BufferedWriter outBufferedWriter = new BufferedWriter(fileWriter1);
        for (Map.Entry<relation,Integer> entry : relationIntegerMap.entrySet()){
            outBufferedWriter.write(entry.getKey() +" "+ entry.getValue() +"\n" );
        }
        fileWriter1.close();
        fileReader.close();
        bufferedReader.close();
    }
}
